﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Controls
{
    public partial class SiteMap : System.Web.UI.UserControl
    {
        public int catID;
        public int parent_id;
        public int id;
        public int flag;
        public int type;
        public int groupID;
        public string url;
        public DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!Page.IsPostBack)
            {

                string[] listurl = Request.Url.AbsoluteUri.Split('/');
                string url = listurl[listurl.Length - 1];
                if (url == "danhmuc.htm")
                {
                    flag = 0;
                    catID = Convert.ToInt32(Request.QueryString["catID"].ToString());
                    DataTable temp = DAL.DCollector.DBCollector.Instance.StoredProcedureGetTableByID("tbl_Category",
                                                                                                   catID,
                                                                                                   "selectcatroot");
                    string[] namecat = temp.Rows[0]["result"].ToString().Split(',');
                    string[] idcat = temp.Rows[0]["catid"].ToString().Split(',');

                    dt.Columns.Add("url");
                    dt.Columns.Add("name");

                    for (int i = namecat.Length - 2; i >= 0; i--)
                    {
                        dt.Rows.Add("Danhmuc" + idcat[i] + "/" + LIB.Converter.convertLink(namecat[i]) + "/danhmuc.htm", namecat[i]);
                    }
                }
                if (url == "danhsachsanpham.htm")
                {
                    flag = 0;
                    catID = Convert.ToInt32(Request.QueryString["catID"].ToString());
                    DataTable temp = DAL.DCollector.DBCollector.Instance.StoredProcedureGetTableByID("tbl_Category",
                                                                                                     catID,
                                                                                                     "selectcatroot");
                    string[] namecat = temp.Rows[0]["result"].ToString().Split(',');
                    string[] idcat = temp.Rows[0]["catid"].ToString().Split(',');

                    dt.Columns.Add("url");
                    dt.Columns.Add("name");

                    for (int i = namecat.Length - 2; i >= 0; i--)
                    {

                        if (i == 0)
                            dt.Rows.Add("/" + LIB.Converter.convertLink(namecat[i]) + "/" + idcat[i] + "/danhsachsanpham.htm", namecat[i]);
                        else
                            dt.Rows.Add("Danhmuc" + idcat[i] + "/" + LIB.Converter.convertLink(namecat[i]) + "/danhmuc.htm", namecat[i]);

                    }
                }
                if (url == "chitietsanpham.htm")
                {
                    flag = 1;
                    id = Convert.ToInt32(Request.QueryString["ID"].ToString());

                    DataTable temp = DAL.DCollector.DBCollector.Instance.StoredProcedureGetTableByID("tbl_Product", id,
                                                                                                     "selectsitemap");
                    string[] namecat = temp.Rows[0]["result"].ToString().Split(',');
                    string[] idcat = temp.Rows[0]["catid"].ToString().Split(',');

                    dt.Columns.Add("url");
                    dt.Columns.Add("name");

                    for (int i = 0; i <= namecat.Length - 2; i++)
                    {

                        if (i == namecat.Length - 2)
                            dt.Rows.Add("/" + LIB.Converter.convertLink(namecat[i]) + "/" + idcat[i] + "/danhsachsanpham.htm", namecat[i]);
                        else
                            dt.Rows.Add("Danhmuc" + idcat[i] + "/" + LIB.Converter.convertLink(namecat[i]) + "/danhmuc.htm", namecat[i]);
                    }
                }

                rpt_sitemap.DataSource = dt;
                rpt_sitemap.DataBind();
            }
        }
    }
}